﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using AjaxControlToolkit;

public partial class Front_UserControl_Login : System.Web.UI.UserControl {
    protected void Page_Load(object sender, EventArgs e) {

        //  setSecureProtocol(true);
        if (!this.IsPostBack) {
            ModalPopupExtender MPECity = (ModalPopupExtender)Page.Master.FindControl("MPECity");
            MPECity.Hide();
            Session["CookiesValue"] = 1;
            ViewState["LoginErrors"] = 0;
        }
    }
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) {

        if (Membership.ValidateUser(Login1.UserName, Login1.Password)) {
            Session["username"] = Login1.UserName;

            e.Authenticated = true;
            Login1.Visible = false;


            //MessageLabel.Text = "Successfully Logged In";            
            //Response.Redirect(Request.QueryString["ReturnUrl"]);
            //Response.Redirect(Request.QueryString["ReturnUrl"].ToString());
            //LinkButton login = this.Page.Master.FindControl("btnLogin") as LinkButton;
            //login.Visible = false;
            //LinkButton logout = this.Page.Master.FindControl("btnLogout") as LinkButton;
            //logout.Visible = false;
            //LinkButton login = (LinkButton)Master.FindControl("btnLogin");
            //login.Visible = false;
            //ContentPlaceHolder MYcontent = (ContentPlaceHolder)this.Page.Master.FindControl("head");
            //LinkButton login = (LinkButton)MYcontent.FindControl("btnLogin");
            //login.Visible = false;
            //Response.Redirect("../DealShow.aspx");

        } else {
            e.Authenticated = false;
            HtmlControl divEmailError = Login1.FindControl("divEmailError") as HtmlControl;
            HtmlControl divPasswordError = Login1.FindControl("divPasswordError") as HtmlControl;

            if (Membership.FindUsersByEmail(Login1.UserName).Count == 0) {
                if (divEmailError != null) divEmailError.Visible = true;
                if (divPasswordError != null) divPasswordError.Visible = false;
            } else {
                if (divEmailError != null) divEmailError.Visible = false;
                if (divPasswordError != null) divPasswordError.Visible = true;
            }
        }
    }



    protected void Login1_LoginError(object sender, EventArgs e) {
        if (ViewState["LoginErrors"] == null)
            ViewState["LoginErrors"] = 0;
        int ErrorCount = (int)ViewState["LoginErrors"] + 1;
        ViewState["LoginErrors"] = ErrorCount;
        if ((ErrorCount > 3) && (Login1.PasswordRecoveryUrl != string.Empty))
            Response.Redirect("PasswordRecovery.aspx");
    }
    //protected void LoginButton_Click(object sender, EventArgs e)
    //{
    //    if (txtUserName.Text == "")
    //    {
    //        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "blankUserName();", true);
    //        txtUserName.Focus();
    //    }

    //    else if (txtPassword.Text == "")
    //    {
    //        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "blankPassword();", true);
    //        txtPassword.Focus();
    //    }

    //    else if (txtUserName.Text != "" && txtPassword.Text != "")
    //    {
    //        try
    //        {
    //            var coupons = new DealesDataContext();
    //            var query = coupons.Insert_Login(txtUserName.Text, txtPassword.Text);
    //            DataTable dt = new DataTable();
    //            foreach (var data in query)
    //            {
    //                dt.Columns.Add(data.RegistrationNo.ToString());
    //                dt.Columns.Add(data.UserName);
    //                dt.Columns.Add(data.Password);
    //                dt.Columns.Add(data.FName);
    //                dt.Columns.Add(data.LName);
    //            }
    //            DataSet ds = new DataSet();
    //            ds.Tables.Add(dt);
    //            if (dt.Columns != null)
    //            {
    //                Session["RegistrationNo"] = dt.Columns[0];
    //                Session["FName"] = dt.Columns[3];
    //                Session["Password"] = dt.Columns[2];
    //                Session["UserName"] = dt.Columns[1];
    //                Session["LName"] = dt.Columns[4];
    //                if (Session["UserName"].ToString() == txtUserName.Text)
    //                {
    //                    Response.Redirect("Home.aspx");
    //                }
    //                else
    //                {
    //                    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "checkuser();", true);
    //                }
    //            }
    //            else
    //            {
    //                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "checkuser();", true);
    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            Response.Write(ex.Message);
    //        }
    //    }

    //}
    protected void btnForgetPassword_Click(object sender, EventArgs e) {
        Response.Redirect("PasswordRecovery.aspx");
    }
    protected void LoginButton_Click(object sender, EventArgs e) {

    }
    //public void setSecureProtocol(bool bSecure)
    //{
    //    string redirectUrl = null;

    //    // if we want HTTPS and it is currently HTTP
    //    if (bSecure && !Request.IsSecureConnection)
    //        redirectUrl = Request.Url.ToString().Replace("http:", "https:");

    //    else

    //        // if we want HTTP and it is currently HTTPS
    //        if (!bSecure && Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("https:", "http:");

    //    //else

    //    // in all other cases we don't need to redirect

    //    // check if we need to redirect, and if so use redirectUrl to do the job
    //    if (redirectUrl != null)

    //        Response.Redirect(redirectUrl);

    //}

}

